package com.pospi.pai.yunpos.vip;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.dto.VipBeen;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.login.Constant;


import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class VipActivity extends Base2Activity implements View.OnClickListener {

    @Bind(R.id.vip_list)
    ListView vip_list;
    @Bind(R.id.tv_search)
    TextView tv_search;
    @Bind(R.id.et_vipNumber)
    EditText et_vipNumber;
//    @Bind(R.id.ll_hyxf)
//    LinearLayout ll_hyxf;


    private VipBeen vipBeen = new VipBeen();
    private List<VipBeen> beens = new ArrayList<>();
    private MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentLayout(R.layout.activity_vipsearch);
        ButterKnife.bind(this);
        setTitle("会员信息查询");
        setMenuImage(R.drawable.menu_add);
        tv_search.setOnClickListener(this);
//        ll_hyxf.setOnClickListener(this);
        adapter = new MyAdapter();
        vip_list.setAdapter(adapter);
        vip_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(VipActivity.this, VIPRegisterActivity.class);
                intent.putExtra("data", beens.get(i));
                startActivity(intent);

            }
        });

    }

    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
        startActivity(VIPRegisterActivity.class);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.ll_hyxf:
//                startActivity(new Intent(this,VipSalesSearchActivity.class));
//                break;
            case R.id.tv_search:
                beens.clear();
                //查询会员
                showDialog();
                Map<String,String> params = new HashMap<>();
                params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
                params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
                params.put("model", "vip.Vvip");
                params.put("fun", "find");
                params.put("table", "vip_info");

                JSONObject object = new JSONObject();
                try {
                    object.put("filter", et_vipNumber.getText().toString().trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                params.put("pds", object.toString());
                new HttpConnection().postNet(params, new HttpCallBackListener() {
                    @Override
                    public void CallBack(String Response) {
                        Log.i("vip", Response);
                        try {
                            JSONObject object = new JSONObject(Response);
                            String code = object.getString("errCode");
                            JSONArray array = object.getJSONArray("result");
                            List<VipBeen> vipBeens = new Gson().fromJson(array.toString(), new TypeToken<List<VipBeen>>() {
                            }.getType());
                            beens.addAll(vipBeens);
                            Message message = Message.obtain();
                            message.what = 1;
                            handler.sendMessage(message);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }

    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    close();

                    if (beens.size() == 0) {
                        Toast.makeText(VipActivity.this, "未查询到数据", Toast.LENGTH_SHORT).show();
                    } else {
                        adapter.notifyDataSetChanged();
                    }


                    break;
                case 2:
                    close();
                    break;
                case 3:
                    close();

                    break;
            }

        }
    };
    private DialogFragment dialog;
    private void showDialog(){
        dialog = new CircleDialog.Builder()
                .setProgressText("查询中...")
                .setProgressStyle(ProgressParams.STYLE_SPINNER)
                .show(getSupportFragmentManager());
    }
    private void close(){
        dialog.dismiss();
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return beens.size();
        }

        @Override
        public Object getItem(int i) {
            return beens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(VipActivity.this).inflate(R.layout.activity_vip_litem, null);
                holder.hym = view.findViewById(R.id.hym);
                holder.hyh = view.findViewById(R.id.hyh);
                holder.jfye = view.findViewById(R.id.jfye);
                holder.mzye = view.findViewById(R.id.mzye);
                holder.hylx = view.findViewById(R.id.hylx);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.hym.setText(beens.get(i).getName());
            holder.hyh.setText(beens.get(i).getNum());
            holder.jfye.setText(beens.get(i).getJfye());
            holder.mzye.setText(beens.get(i).getMzye());
            holder.hylx.setText(beens.get(i).getTypename());

            return view;
        }
    }
    class Holder{
        TextView hym;
        TextView hyh;
        TextView jfye;
        TextView mzye;
        TextView hylx;
    }
}
